﻿using MESMian.Context;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MESMian.Forms
{
    public partial class StoreList : Form
    {
        public StoreList()
        {
            InitializeComponent();
            ucPager1.PagerEvent += UcPager1_PagerEvent;
        }

        private void UcPager1_PagerEvent(int currentPage, int pageSize, out int totalCount)
        {
            using (var mtx = new MyContext())
            {
                var query = from s in mtx.Stores
                            orderby s.StoreId ascending
                            join u in mtx.Users on s.StoreId equals u.UserId
                            join sc in mtx.StoreCategories on s.StoreId equals sc.StoreCategoryId
                            //-------------------------------------
                            join u1 in mtx.Users on s.CreateUserId equals u1.CreateUserId
                            join u2 in mtx.Users on s.LastUpdateUserId equals u2.UserId
                            select new
                            {
                                s.StoreId,
                                仓库名称 = s.StoreName,
                                仓库编号 = s.StoreCode,
                                s.Storekeeper,
                                仓管员 = u.UserName,
                                联系方式=u.Phone,
                                仓库地址=s.StoreAddress,
                                仓库类型 = sc.StoreCategoryName,
                                状态=s.Status==0?"正常":"禁用",
                                s.CreateUserId,
                                创建人 = u1.UserName,
                                创建时间 = s.CreateTime,
                                s.LastUpdateUserId,
                                修改人= u2!=null? u2.UserName:null,
                                修改时间 =s.LastUpdateTime,
                            };

                totalCount = query.Count();

                int skipNum = (currentPage - 1) * pageSize;
                var data = query.Skip(skipNum).Take(pageSize).ToListAsync();
                dataGridView1.DataSource = data;
            }
        }
    }
}
